package com.example.demo2.demo05;

import com.example.demo2.rabbitmq.RabbitMQConnection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description: 生产者-交换机类型-direct  路由模式-路由键为 email
 * 当交换机类型为direct类型时，根据队列绑定的路由建转发到具体的队列中存放消息
 * @Author: mingtian
 * @CreateDate: 2021/1/16 10:05
 * @Version: 1.0
 */
public class ProducerDirect {

    /**
     * 定义交换机的名称
     */
    private static final String EXCHANGE_NAME = "direct_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        //  创建Connection
        Connection connection = RabbitMQConnection.getConnection();
        // 创建Channel
        Channel channel = connection.createChannel();
        // 通道关联交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "direct", true);
        String msg = "hello,this is msg";
        // 投递消息给交换机 路由键(email)
        channel.basicPublish(EXCHANGE_NAME, "email", null, msg.getBytes());
        channel.close();
        connection.close();
    }

}